package com.ziytek.web.citizen.controller;

import com.google.gson.Gson;
import com.ziytek.web.citizen.constant.ApiConstant;
import com.ziytek.web.citizen.constant.NumConstant;
import com.ziytek.web.citizen.enums.ResponseEnum;
import com.ziytek.web.citizen.enums.SchmeEnum;
import com.ziytek.web.citizen.model.req.*;
import com.ziytek.web.citizen.model.rsp.UnifyResponse;
import com.ziytek.web.citizen.service.itf.AdminService;
import com.ziytek.web.citizen.service.itf.CabinetIpService;
import com.ziytek.web.citizen.service.itf.CapacitySchemeService;
import com.ziytek.web.citizen.service.itf.LocalCapacityService;
import com.ziytek.web.citizen.util.UuidUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 电池分容接口
 * @author fenghx
 * @version 1.0
 * @className SettingController
 * Description:设置接口
 * @date 2023/12/7 16:46
 **/
@Slf4j
@RestController
@RequestMapping(ApiConstant.API_CABINET)
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SettingController {
    private final Gson gson;
    private final AdminService adminService;
    private final CabinetIpService cabinetIpService;
    private final LocalCapacityService localCapacityService;
    private final CapacitySchemeService capacitySchemeService;
//
//    //@Operation(summary = "电池分容后灯控显示接口(灯控接口)")
//    @PostMapping(ApiConstant.LIGHT_CONTROL_DISPLAY)
//    public UnifyResponse lightControl(@RequestBody LightControlReq request) {
//        log.info("LightControl request:{}", gson.toJson(request));
//        long start = System.currentTimeMillis();
//        UnifyResponse response = lightControlService.lightControl(request);
//        log.info("LightControl response:{},耗时:{}ms", response, System.currentTimeMillis() - start);
//        return response;
//    }

    /**
     * 分容柜网络IP地址设置接口
     *
     * @param request 请求参数
     * @return 分容柜网络IP地址设置响应
     */
    @PostMapping(ApiConstant.IP_SET)
    public UnifyResponse ipSet(@RequestBody IpSetReq request) {
        var response = new UnifyResponse();
        response.setRetCode(ResponseEnum.OTHER.getCode());
        response.setRetMsg(ResponseEnum.OTHER.getDesc());
        log.info("ipSet request:{}", gson.toJson(request));

        var hasPermission = adminService.hasAdminPermission(request.getToken());
        if (!hasPermission) {
            response.setRetCode(ResponseEnum.NO_PERMISSION.getCode());
            response.setRetMsg(ResponseEnum.NO_PERMISSION.getDesc());
            return response;
        }

        var success = cabinetIpService.saveAndUpdate(request);
        if (success > NumConstant.INT_ZERO) {
            response.setRetCode(ResponseEnum.SUCCESS.getCode());
            response.setRetMsg(ResponseEnum.SUCCESS.getDesc());
        }

        return response;
    }

    /**
     * 分容设置方案新增接口
     *
     * @param request 请求参数
     * @return 分容设置方案新增响应
     */
    @PostMapping(ApiConstant.CAPACITY_SCHEME_ADD)
    public UnifyResponse capacitySchemeAdd(@RequestBody CapacitySchemeAddReq request) {
        var response = new UnifyResponse();
        response.setRetCode(ResponseEnum.OTHER.getCode());
        response.setRetMsg(ResponseEnum.OTHER.getDesc());
        log.info("New capacity setting scheme for capacity distribution cabinets:{}", gson.toJson(request));

        var hasPermission = adminService.hasAdminPermission(request.getToken());
        if (!hasPermission) {
            response.setRetCode(ResponseEnum.NO_PERMISSION.getCode());
            response.setRetMsg(ResponseEnum.NO_PERMISSION.getDesc());
            return response;
        }

        var code = capacitySchemeService.schemeAdd(request, null, true);
        response.setRetCode(code);
        response.setRetMsg(SchmeEnum.SUCCESS.getMsg(code));
        return response;
    }

    /**
     * 分容设置方案修改接口
     *
     * @param request 请求参数
     * @return 分容设置方案修改响应
     */
    @PostMapping(ApiConstant.CAPACITY_SCHEME_MODIFY)
    public UnifyResponse capacitySchemeModify(@RequestBody CapacitySchemeModifyReq request) {
        var response = new UnifyResponse();
        response.setRetCode(ResponseEnum.OTHER.getCode());
        response.setRetMsg(ResponseEnum.OTHER.getDesc());
        log.info("Modification of the Capacity Setting Scheme for the Capacity Distribution Cabinet:{}", gson.toJson(request));

        var hasPermission = adminService.hasAdminPermission(request.getToken());
        if (!hasPermission) {
            response.setRetCode(ResponseEnum.NO_PERMISSION.getCode());
            response.setRetMsg(ResponseEnum.NO_PERMISSION.getDesc());
            return response;
        }

        var code = capacitySchemeService.schemeModify(request);
        response.setRetCode(code);
        response.setRetMsg(SchmeEnum.SUCCESS.getMsg(code));
        return response;
    }

    /**
     * 分容设置方案删除接口
     *
     * @param request 请求参数
     * @return 分容设置方案删除响应
     */
    @PostMapping(ApiConstant.CAPACITY_SCHEME_DEL)
    public UnifyResponse capacitySchemeDel(@RequestBody CapacitySchemeDelReq request) {
        log.info("Delete the capacity setting scheme for the capacity distribution cabinet:{}", gson.toJson(request));
        var response = new UnifyResponse();
        response.setRetCode(ResponseEnum.OTHER.getCode());
        response.setRetMsg("删除失败");
        log.info("capacitySchemeDel request:{}", request);

        var hasPermission = adminService.hasAdminPermission(request.getToken());
        if (!hasPermission) {
            response.setRetCode(ResponseEnum.NO_PERMISSION.getCode());
            response.setRetMsg(ResponseEnum.NO_PERMISSION.getDesc());
            return response;
        }

        var code = capacitySchemeService.schemeDel(request.getId());
        if (code > NumConstant.INT_ZERO) {
            response.setRetCode(ResponseEnum.OTHER.getCode());
            response.setRetMsg("删除失败");
        }

        response.setRetCode(ResponseEnum.SUCCESS.getCode());
        response.setRetMsg(ResponseEnum.SUCCESS.getDesc());
        return response;
    }

    /**
     * 本地分容结果查询指令下发接口
     *
     * @param request 请求参数
     * @return 本地分容结果查询指令下发响应
     */
    @PostMapping(ApiConstant.LOCAL_DEVICE_CAPACITY_ISSUED)
    public UnifyResponse localDeviceCapacityIssued(@RequestBody LocalDeviceCapacityIssuedReq request) {
        var requestId = UuidUtil.getUuid();
        log.info("Interface for issuing query instructions for local capacity division results of the capacity division cabinet:{},requestId:{}", gson.toJson(request), requestId);
        return localCapacityService.localDeviceCapacityIssued(request.getCabinetNo());
    }

    //@Operation(summary = "历史数据库选择切换接口")
    /**
     * 历史数据库选择切换接口(已弃用)
     * @param request 请求参数
     * @return 历史数据库选择切换响应
     * */
    @PostMapping(ApiConstant.HISTORY_DB_SWITCH)
    public UnifyResponse historyDbSwitch(@RequestBody HistoryDbSwitchReq request) {
        String requestId = UuidUtil.getUuid();
        log.info("Historical database selection switching request:{},requestId:{}", gson.toJson(request), requestId);
        UnifyResponse response = new UnifyResponse();
        response.setRetCode(ResponseEnum.SUCCESS.getCode());
        response.setRetMsg(ResponseEnum.SUCCESS.getDesc());
        return response;
        //return historyDbSwitchService.switchHistoryDb(request.getHistoryDbFileName());
    }
}
